//
//  StyleViewController.m
//  BasketballClub
//
//  Created by 孙鸿吉 on 16/1/18.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "StyleViewController.h"
#import "StyleView.h"
#import <AVOSCloud.h>
#import "LeanCloudService.h"
@interface StyleViewController ()<UIGestureRecognizerDelegate,UITextFieldDelegate>
@property (nonatomic,strong)StyleView *styleView;
@end

@implementation StyleViewController
- (void)loadView
{
    self.styleView = [[StyleView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.styleView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.styleView.backgroundColor =[UIColor colorWithRed:240/255.0 green:240/255.0 blue:242/255.0 alpha:1.0];
    //遵守代理
    self.styleView.rePasswordTF.delegate = self;
    self.styleView.nePasswordTF.delegate = self;
    //返回按钮
    [self.styleView.cancleButton addTarget:self action:@selector(returnBeforeAction) forControlEvents:UIControlEventTouchUpInside];
    //昵称
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushNickView)];
    self.styleView.nickNameView.userInteractionEnabled = YES;
    tapGR.delegate = self;
    [self.styleView.nickNameView addGestureRecognizer:tapGR];
    //个性签名
    UITapGestureRecognizer *styleGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushStyleView)];
    self.styleView.styleTextView.userInteractionEnabled = YES;
    styleGR.delegate = self;
    [self.styleView.styleTextView addGestureRecognizer:styleGR];
    //修改密码
    UITapGestureRecognizer *changeGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changePWDAction)];
    self.styleView.changePWDView.userInteractionEnabled = YES;
    changeGR.delegate = self;
    [self.styleView.changePWDView addGestureRecognizer:changeGR];
    //取消按钮(昵称)
    [self.styleView.cancleButton1 addTarget:self action:@selector(cancleButton1Action) forControlEvents:UIControlEventTouchUpInside];
    //保存按钮(昵称)
    [self.styleView.saveButton1 addTarget:self action:@selector(saveButton1Action) forControlEvents:UIControlEventTouchUpInside];
    //取消按钮(个性签名)
    [self.styleView.cancleButton2 addTarget:self action:@selector(cancleButton2Action) forControlEvents:UIControlEventTouchUpInside];
    //取保存按钮(个性签名)
    [self.styleView.saveButton2 addTarget:self action:@selector(saveButton2Action) forControlEvents:UIControlEventTouchUpInside];
    //取消按钮(修改密码)
    [self.styleView.cancleButton3 addTarget:self action:@selector(cancleButton3Action) forControlEvents:UIControlEventTouchUpInside];
    //保存按钮(修改密码)
    [self.styleView.saveButton3 addTarget:self action:@selector(saveButton3Action) forControlEvents:UIControlEventTouchUpInside];
    //退出登录
    [self.styleView.logoutButton addTarget:self action:@selector(logoutButtonAction) forControlEvents:UIControlEventTouchUpInside];
    //错误1按钮
    [self.styleView.errorButton1 addTarget:self action:@selector(errorButton1Action) forControlEvents:UIControlEventTouchUpInside];
    //错误2按钮
    [self.styleView.errorButton2 addTarget:self action:@selector(errorButton2Action) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 退出登录
- (void)logoutButtonAction
{
    [AVUser logOut];
    [self logoutSuccess];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
#pragma mark - 取消按钮方法
- (void)returnBeforeAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 昵称点击方法
- (void)pushNickView
{

    [UIView animateWithDuration:0.3f animations:^{
        self.styleView.nickNameBackView.center = self.view.center;
    }];
}
#pragma mark - 个性签名点击方法
- (void)pushStyleView
{
    [UIView animateWithDuration:0.3f animations:^{
        self.styleView.styleBackView.center = self.view.center;
    }];
}
#pragma mark - 修改密码点击方法
- (void)changePWDAction
{
    [UIView animateWithDuration:0.3f animations:^{
        self.styleView.changeBackView.frame = CGRectMake((SCREEN_WIDTH - 320 * LAYOUT_SIZE)/2, (SCREEN_HEIGHT - 320 * LAYOUT_SIZE_HEIGHT)/2, 320 * LAYOUT_SIZE, 270 * LAYOUT_SIZE_HEIGHT);
    }];
}
#pragma mark - 取消按钮(昵称)
- (void)cancleButton1Action
{
    [UIView animateWithDuration:0.3f animations:^{
        self.styleView.nickNameBackView.frame = CGRectMake(-320 * LAYOUT_SIZE, (SCREEN_HEIGHT - 200 * LAYOUT_SIZE_HEIGHT)/2, 320 * LAYOUT_SIZE, 200 * LAYOUT_SIZE_HEIGHT);
    }];
    [self.styleView endEditing:YES];
}
#pragma mark - 保存按钮(昵称)
- (void)saveButton1Action
{
    AVUser *current = [AVUser currentUser];
    [current setObject:self.styleView.nickNameTF.text forKey:@"styleNickName"];
    [current signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        
    }];
    [UIView animateWithDuration:0.3f animations:^{
        self.styleView.nickNameBackView.frame = CGRectMake(-320 * LAYOUT_SIZE, (SCREEN_HEIGHT - 200 * LAYOUT_SIZE_HEIGHT)/2, 320 * LAYOUT_SIZE, 200 * LAYOUT_SIZE_HEIGHT);
    }];
    [self.styleView endEditing:YES];
}
#pragma mark - 取消按钮(个性签名)
- (void)cancleButton2Action
{
    [UIView animateWithDuration:0.3f animations:^{
        self.styleView.styleBackView.frame = CGRectMake(47 * LAYOUT_SIZE, SCREEN_HEIGHT, 320 * LAYOUT_SIZE, 200 * LAYOUT_SIZE_HEIGHT);
    }];
    [self.styleView endEditing:YES];
}
#pragma mark - 保存按钮(个性签名)
- (void)saveButton2Action
{
    AVUser *current = [AVUser currentUser];
    [current setObject:self.styleView.styleTF.text forKey:@"styleName"];
    [current signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        
    }];
    [UIView animateWithDuration:0.3f animations:^{
        self.styleView.styleBackView.frame = CGRectMake(47 * LAYOUT_SIZE, SCREEN_HEIGHT, 320 * LAYOUT_SIZE, 200 * LAYOUT_SIZE_HEIGHT);
    }];
    [self.styleView endEditing:YES];
}
#pragma mark - 取消按钮(修改密码)
- (void)cancleButton3Action
{
    [UIView animateWithDuration:0.3f animations:^{
        self.styleView.changeBackView.frame = CGRectMake(SCREEN_WIDTH, (SCREEN_HEIGHT - 270 * LAYOUT_SIZE_HEIGHT)/2, 320 * LAYOUT_SIZE, 270 * LAYOUT_SIZE_HEIGHT);
    }];
    [self.styleView endEditing:YES];
}
#pragma mark - 保存按钮(修改密码)
- (void)saveButton3Action
{
    if (self.styleView.nePasswordTF.text.length == 0 || self.styleView.changeTF.text.length == 0 || self.styleView.rePasswordTF.text.length == 0){
        
    }
    if ([self.styleView.nePasswordTF.text isEqualToString:self.styleView.rePasswordTF.text]) {
        AVUser *current = [AVUser currentUser];
        [AVUser logInWithUsername:current.username password:self.styleView.changeTF.text error:nil];
        [[AVUser currentUser] updatePassword:self.styleView.changeTF.text newPassword:self.styleView.nePasswordTF.text block:^(id object, NSError *error) {
            if (!error) {
                [self changePasswordSuccess];
                self.styleView.changeTF.text = nil;
                self.styleView.rePasswordTF.text = nil;
                self.styleView.nePasswordTF.text = nil;
                [UIView animateWithDuration:0.3f animations:^{
                    self.styleView.changeBackView.frame = CGRectMake(SCREEN_WIDTH, (SCREEN_HEIGHT - 270 * LAYOUT_SIZE_HEIGHT)/2, 320 * LAYOUT_SIZE, 270 * LAYOUT_SIZE_HEIGHT);
                }];
                
            }else{
                self.styleView.changeTF.text = nil;
                self.styleView.rePasswordTF.text = nil;
                self.styleView.nePasswordTF.text = nil;
                [self changePasswordFiled];
                
            }
        }];

    }
    [self.styleView endEditing:YES];
}


#pragma mark - 提示框(修改密码成功)
- (void)changePasswordSuccess
{
    CGFloat verison = [[[UIDevice currentDevice]systemVersion]floatValue];
    if (verison >= 8.0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码修改成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"密码修改成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];

        [self performSelector:@selector(aftertimeAction:) withObject:alertView afterDelay:2.0];
    }

}


#pragma mark - 提示框(修改密码失败)
- (void)changePasswordFiled
{
    CGFloat verison = [[[UIDevice currentDevice]systemVersion]floatValue];
    if (verison >= 8.0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码修改失败,请确认当前账户密码是否正确" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"密码修改失败,请确认当前账户密码是否正确" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
        
        [self performSelector:@selector(aftertimeAction:) withObject:alertView afterDelay:2.0];
    }
    

}
#pragma mark - 提示框(退出登录成功)
- (void)logoutSuccess
{
    CGFloat verison = [[[UIDevice currentDevice]systemVersion]floatValue];
    if (verison >= 8.0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"退出登录成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"退出登录成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
        
        [self performSelector:@selector(aftertimeAction:) withObject:alertView afterDelay:2.0];
    }

}
#pragma mark - 提示框(密码不能为空)
- (void)passwordNotNull
{
    CGFloat verison = [[[UIDevice currentDevice]systemVersion]floatValue];
    if (verison >= 8.0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"密码不能为空" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
        
        [self performSelector:@selector(aftertimeAction:) withObject:alertView afterDelay:2.0];
    }
    
}

#pragma mark - 延迟2秒方法
- (void)aftertimeAction:(UIAlertView *)sender
{
    [sender dismissWithClickedButtonIndex:0 animated:YES];
    
}

#pragma mark - 错误1按钮方法实现
- (void)errorButton1Action
{
    [UIView animateWithDuration:0.3f animations:^{
        self.styleView.errorImageView1.frame = CGRectMake(240 * LAYOUT_SIZE, 85 * LAYOUT_SIZE_HEIGHT, 80 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.styleView.errorImageView1.frame = CGRectMake(240 * LAYOUT_SIZE, 85 * LAYOUT_SIZE_HEIGHT, 0, 0);
        });
    }];
}

#pragma mark - 错误2按钮方法实现
- (void)errorButton2Action
{
    [UIView animateWithDuration:0.3f animations:^{
        self.styleView.errorImageView2.frame = CGRectMake(240 * LAYOUT_SIZE, 135 * LAYOUT_SIZE_HEIGHT, 80 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.styleView.errorImageView2.frame = CGRectMake(240 * LAYOUT_SIZE, 85 * LAYOUT_SIZE_HEIGHT, 0, 0);
        });
    }];
}


#pragma mark - 监控键盘
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([textField isEqual:self.styleView.nePasswordTF]) {
        if (self.styleView.nePasswordTF.text.length < 6) {
            self.styleView.errorButton1.hidden = NO;
            [self.styleView.errorButton1 setImage:[UIImage imageNamed:@"iconfont-tanhao"] forState:UIControlStateNormal];
        }else{
            self.styleView.errorButton1.hidden = NO;
            [self.styleView.errorButton1 setImage:[UIImage imageNamed:@"iconfont-duihao"] forState:UIControlStateNormal];
        }
    }
    
    if ([textField isEqual:self.styleView.rePasswordTF]) {
        if ([self.styleView.rePasswordTF.text isEqualToString:self.styleView.nePasswordTF.text]) {
            self.styleView.errorButton2.hidden = NO;
            [self.styleView.errorButton2 setImage:[UIImage imageNamed:@"iconfont-duihao"] forState:UIControlStateNormal];
        }else{
            self.styleView.errorButton2.hidden = NO;
            [self.styleView.errorButton2 setImage:[UIImage imageNamed:@"iconfont-tanhao"] forState:UIControlStateNormal];
        }
    }
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
